// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'ljmx2',
  traceUser: true
})

// 云函数入口函数
exports.main = async (e, context) => {
  var openId = e.userInfo.openId
  var name1 = e.name
  var event1 = e.event
  var money1 = e.money
  var key = e.key
  var i = e.i
  if (key == 0) {
    if (name1 == '' & event1 == '' & money1 == '') {
      return cloud.database().collection("add").where({
        _openid: openId
      }).limit(50).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 == '' & event1 == '' & money1 != '') {
      return cloud.database().collection("add").where({
        money: cloud.database().RegExp({
          regexp:'.*'+ money1, //做为关键字进行匹配
          options: 'i', //不区分大小写
        }),
        _openid: openId
      }).limit(50).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 == '' & event1 != '' & money1 == '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        event: cloud.database().RegExp({
          regexp:'.*'+ event1, //做为关键字进行匹配
          options: 'i', //不区分大小写
        }),
        
      }).limit(50).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 != '' & event1 == '' & money1 == '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        name: cloud.database().RegExp({
          regexp:'.*'+ name1, //做为关键字进行匹配
          options: 'i', //不区分大小写
        }),

      }).limit(50).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 != '' & event1 != '' & money1 == '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        name: name1,
        event: event1
      }).limit(50).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 != '' & event1 == '' & money1 != '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        name: name1,
        money: money1
      }).limit(50).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 == '' & event1 != '' & money1 != '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        event: event1,
        money: money1
      }).limit(50).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 != '' & event1 != '' & money1 != '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        event: event1,
        money: money1,
        name: name1
      }).limit(50).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
  }
  if (key == 1) {
    if (name1 == '' & event1 == '' & money1 == '') {
      return cloud.database().collection("add").where({
        _openid: openId
      }).limit(50).skip(50 * i).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 == '' & event1 == '' & money1 != '') {
      return cloud.database().collection("add").where({
        money: cloud.database().RegExp({
          regexp:'.*'+ money1, //做为关键字进行匹配
          options: 'i', //不区分大小写
        }),
        _openid: openId
      }).limit(50).skip(50 * i).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 == '' & event1 != '' & money1 == '') {
      return cloud.database().collection("add").where({
        event: cloud.database().RegExp({
          regexp:'.*'+ event1, //做为关键字进行匹配
          options: 'i', //不区分大小写
        }),
        _openid: openId
      }).limit(50).skip(50 * i).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 != '' & event1 == '' & money1 == '') {
      return cloud.database().collection("add").where({
        name: cloud.database().RegExp({
          regexp:'.*'+ name1, //做为关键字进行匹配
          options: 'i', //不区分大小写
        }),
        _openid: openId
      }).limit(50).skip(50 * i).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 != '' & event1 != '' & money1 == '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        name: name1,
        event: event1
      }).limit(50).skip(50 * i).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 != '' & event1 == '' & money1 != '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        name: name1,
        money: money1
      }).limit(50).skip(50 * i).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 == '' & event1 != '' & money1 != '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        event: event1,
        money: money1
      }).limit(50).skip(50 * i).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
    if (name1 == '' & event1 != '' & money1 != '') {
      return cloud.database().collection("add").where({
        _openid: openId,
        event: event1,
        money: money1,
        name: name1
      }).limit(50).skip(50 * i).get({
        success: function (res) {
          let length = res.data.length
          var all = [];
          for (let i = 0; i < length; i++) {
            var id = res.data[i]._id
            var name = res.data[i].name
            var money = res.data[i].money
            var remarks = res.data[i].remarks
            var event = res.data[i].event
            var all = all.concat({
              id,
              name,
              money,
              remarks,
              event
            })
          }
          return all
        },
        fail: function (err) {
          return err
        }
      })
    }
  }

}